﻿using System;
using System.Collections;
using System.Web;

namespace PlatypusPlanner.Components.Utilities
{
    public static class CacheUtility
    {
        public static void AddToCache(string keyName, object obj, double minutesToExpire)
        {
            if (HttpContext.Current.Cache.Get(keyName) != null)
                HttpContext.Current.Cache.Remove(keyName);

            HttpContext.Current.Cache.Add(keyName, obj, null, DateTime.Now.AddMinutes(minutesToExpire),
                TimeSpan.FromMinutes(0), System.Web.Caching.CacheItemPriority.Normal, null);
        }
        public static T GetFromCache<T>(string keyName) where T : class
        {
            return HttpContext.Current.Cache.Get(keyName) as T;
        }
        public static void RemoveFromCache(string keyName)
        {
            HttpContext.Current.Cache.Remove(keyName);
        }
        public static void RemoveFromCacheWhereContains(string keyname)
        {
            IDictionaryEnumerator cacheEnum = HttpContext.Current.Cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                if (cacheEnum.Key.ToString().Contains(keyname))
                    HttpContext.Current.Cache.Remove(cacheEnum.Key.ToString());
            }
        }
    }
}
